package GUI;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;


public class personaliseWindow extends Composite {

	private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
	private Text QuickAdd;
	private Text QuickShow;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public personaliseWindow(Composite parent, int style) {
		super(parent, style);
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				toolkit.dispose();
			}
		});
		toolkit.adapt(this);
		toolkit.paintBordersFor(this);		
		
		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setBounds(10, 28, 72, 20);
		toolkit.adapt(lblNewLabel, true, true);
		lblNewLabel.setText("Quick Add :");
		
		Label lblNewLabel_1 = new Label(this, SWT.NONE);
		lblNewLabel_1.setBounds(10, 65, 72, 20);
		toolkit.adapt(lblNewLabel_1, true, true);
		lblNewLabel_1.setText("Quick Show : ");
		
	
		
		Label lblPleaseEnsreThat = new Label(this, SWT.NONE);
		lblPleaseEnsreThat.setBounds(10, 0, 408, 15);
		toolkit.adapt(lblPleaseEnsreThat, true, true);
		lblPleaseEnsreThat.setText("Please ensure that the hotkey selected does not clash with other applications");
		
		Button btnRestoreToDefault = new Button(this, SWT.NONE);
		btnRestoreToDefault.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				QuickAdd.setText("A");
				QuickShow.setText("S");
			}
		});
		btnRestoreToDefault.setBounds(10, 101, 106, 25);
		toolkit.adapt(btnRestoreToDefault, true, true);
		btnRestoreToDefault.setText("Restore to Default");
		
		Label lblCtrlShift = new Label(this, SWT.NONE);
		lblCtrlShift.setBounds(88, 28, 76, 20);
		toolkit.adapt(lblCtrlShift, true, true);
		lblCtrlShift.setText("Ctrl + Shift +");
		
		QuickAdd = new Text(this, SWT.BORDER);
		QuickAdd.setBounds(170, 28, 29, 21);
		toolkit.adapt(QuickAdd, true, true);
		
		Label label = new Label(this, SWT.NONE);
		label.setText("Ctrl + Shift +");
		label.setBounds(88, 65, 76, 20);
		toolkit.adapt(label, true, true);
		
		QuickShow = new Text(this, SWT.BORDER);
		QuickShow.setBounds(170, 65, 29, 21);
		toolkit.adapt(QuickShow, true, true);
		
		File inFile = createFile();
		
		
		
		try {
			
			LineNumberReader fileReader = new LineNumberReader(new FileReader(inFile));
			for(int i=0;i<2;i++)
			{
				StringTokenizer seperateDelimeter = new StringTokenizer(fileReader.readLine());
				seperateDelimeter.nextToken();
				String hotkey = seperateDelimeter.nextToken();
				if(i==0)
				{
					QuickAdd.setText(hotkey);
				}
				else if(i==1)
				{
					QuickShow.setText(hotkey);
				}
			}
			
		}catch (IOException e1)
		{
		} 
		
		final Label outputMessage = new Label(this, SWT.NONE);
		outputMessage.setBounds(10, 129, 398, 31);
		toolkit.adapt(outputMessage, true, true);
		
		Button btnSaveChanges = new Button(this, SWT.NONE);
		btnSaveChanges.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					PrintWriter fileWriter = new PrintWriter(createFile());
					String[] hotkeys = new String[2];
					hotkeys[0] = QuickAdd.getText();
					hotkeys[1] = QuickShow.getText();
					
					//TODO error checking of QuickAdd and QUickShow input
					
					fileWriter.println("QuickAdd= "+hotkeys[0]);
					fileWriter.println("QuickShow= "+hotkeys[1]);
					
					fileWriter.println("\n\n\n");
					fileWriter.println("############################################################");
					fileWriter.println("Warning: Please Do not Edit this file \n Editing of this file might render your application to not work");
					fileWriter.println("############################################################");
					
					fileWriter.flush();
					fileWriter.close();
					outputMessage.setText("HotKey configuration changed.\n Please reset the program to apply the change.");
					
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		btnSaveChanges.setBounds(315, 98, 93, 25);
		toolkit.adapt(btnSaveChanges, true, true);
		btnSaveChanges.setText("Save changes");
		

		
		/*
		
		PrintWriter fileWriter = null;
		try {
			fileWriter = new PrintWriter(inFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		
		fileWriter.println("hehe");
		fileWriter.flush();
		fileWriter.close();
		*/

	}

	private File createFile() {
		File inFile = null;
		try {
			inFile = new File("configurationHotKeys.txt");
			inFile.createNewFile();
		} catch (IOException e) {
			System.out.println(e);
		}
		return inFile;
	}
	
	public static void main(String[] args)
	{
	    Display display = new Display();
	    Shell shell = new Shell(display);
	    personaliseWindow calc = new personaliseWindow(shell, SWT.NONE);
	    calc.pack();
	    shell.pack();
	    shell.open();
	    while(!shell.isDisposed()){
	        if(!display.readAndDispatch()) display.sleep();
	    }
	} 

}

